package com.anjho.pojo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "product")
@NamedQueries(value = {
		@NamedQuery(name = "Product.getProductList", query = "SELECT p FROM Product p"),
		@NamedQuery(name = "Product.getProductListWhereReturnIsNull", query = "SELECT p FROM Product p WHERE p.returnDate IS NULL"),
		@NamedQuery(name = "Product.getProductByBarcode", query = "SELECT p FROM Product p WHERE p.barcode = :barcode"),
		@NamedQuery(name = "Product.getProductByNumber", query = "SELECT p FROM Product p WHERE p.proNumber = :proNumber"),
		@NamedQuery(name = "Product.getProductByCover", query = "SELECT p FROM Product p WHERE p.cover = :cover"),
		@NamedQuery(name = "Product.getProductByBarcodeAndNumber", query = "SELECT p FROM Product p WHERE p.barcode = :barcode AND p.proNumber= :proNumber") })
public class Product implements Serializable {
	
	private static final long serialVersionUID = 5405728628984307490L;

	public Product(){
		this.setDistributor(new Distributor());
		this.setGenre(new Genre());
		this.setPublisher(new Publisher());
		this.setReceiptDate(new Date());
		this.setReturnDate(new Date());
	}

	@Id
	@Column(name="prcode")
	@OneToMany(mappedBy="product")
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private long prcode;
	
	@Column(name = "barcode", length = 23, nullable = false)
	private String barcode;

	@Column(name = "pronumber", length = 5, nullable = true)
	private String proNumber;

	@Column(name = "name", nullable = false, length = 50)
	private String name;

	@Column(name = "cover", nullable = false, length = 100)
	private String cover;

	@Column(name = "price", nullable = false, precision = 10, scale=2)
	private BigDecimal price;

	@Column(name = "qtd", nullable = false)
	private int qtd;

	@Column(name = "receivedQtd", nullable = false)
	private int receivedQtd;

	@Temporal(TemporalType.DATE)
	@Column(name = "receiptDate", nullable = false)
	private Date receiptDate;

	@Temporal(TemporalType.DATE)
	@Column(name = "returnDate", nullable = true)
	private Date returnDate;

	@ManyToOne
	@JoinColumn(name = "pucode", nullable = true)
	private Publisher publisher;

	@ManyToOne
	@JoinColumn(name = "dicode", nullable = true)
	private Distributor distributor;

	@ManyToOne
	@JoinColumn(name = "gecode", nullable = true)
	private Genre genre;

	public long getPrcode() {
		return prcode;
	}

	public void setPrcode(long prcode) {
		this.prcode = prcode;
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public String getProNumber() {
		return proNumber;
	}

	public void setProNumber(String proNumber) {
		this.proNumber = proNumber;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCover() {
		return cover;
	}

	public void setCover(String cover) {
		this.cover = cover;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getQtd() {
		return qtd;
	}

	public void setQtd(int qtd) {
		this.qtd = qtd;
	}

	public int getReceivedQtd() {
		return receivedQtd;
	}

	public void setReceivedQtd(int receivedQtd) {
		this.receivedQtd = receivedQtd;
	}

	public Date getReceiptDate() {
		return receiptDate;
	}

	public void setReceiptDate(Date receiptDate) {
		this.receiptDate = receiptDate;
	}

	public Date getReturnDate() {
		return returnDate;
	}

	public void setReturnDate(Date returnDate) {
		this.returnDate = returnDate;
	}

	public Publisher getPublisher() {
		return publisher;
	}

	public void setPublisher(Publisher publisher) {
		this.publisher = publisher;
	}

	public Distributor getDistributor() {
		return distributor;
	}

	public void setDistributor(Distributor distributor) {
		this.distributor = distributor;
	}

	public Genre getGenre() {
		return genre;
	}

	public void setGenre(Genre genre) {
		this.genre = genre;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((barcode == null) ? 0 : barcode.hashCode());
		result = prime * result
				+ ((proNumber == null) ? 0 : proNumber.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Product other = (Product) obj;
		if (barcode == null) {
			if (other.barcode != null)
				return false;
		} else if (!barcode.equals(other.barcode))
			return false;
		if (proNumber == null) {
			if (other.proNumber != null)
				return false;
		} else if (!proNumber.equals(other.proNumber))
			return false;
		return true;
	}

}
